﻿//-------------------------------------------------------------------------------------------------------------------------------------
// Copyright (C) 2005 greatelsoft.com All Rights Reserved
//-------------------------------------------------------------------------------------------------------------------------------------

using System;
using System.Data.Common;
using System.Data;
using System.Data.OleDb;
using System.Collections.Generic;
using System.Text;
using System.Linq;
using GTS.MiniBox.Entity;
using Pub.Class;

namespace GTS.MiniBox.Helper {
    /// <summary>
    /// 厂商操作类
    /// 
    /// 修改纪录
    ///     2012-08-30 版本：1.0 系统自动创建此类
    /// 
    /// </summary>
    public partial class FactoryInfoHelper {
        /// <summary>
        /// 厂商缓存多少秒 x 5
        /// </summary>
        public static int cacheSeconds = 1440;
        /// <summary>
        /// 厂商添加记录
        /// </summary>
        /// <param name="factoryInfo">厂商实体类</param>
        /// <param name="delCache">添加成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>返回添加成功后的ID</returns>
        public static int Insert(FactoryInfo factoryInfo, string dbkey = "", string[] delCache = null) {
            string strSql = new SQL().Insert(FactoryInfo._)
                .Value(FactoryInfo._FactoryID, factoryInfo.FactoryID)
                .Value(FactoryInfo._Manufacturers, factoryInfo.Manufacturers)
                .Value(FactoryInfo._IsMoney, factoryInfo.IsMoney)
                .ToString();
            strSql += ";select SCOPE_IDENTITY();";
            object objValue = dbkey.IsNullEmpty() ? Data.GetScalar(strSql) : Data.Pool(dbkey).GetScalar(strSql);
            if (objValue.IsAllNull()) return 0;
            int value = objValue.ToInt();
            if (delCache.IsNull()) return value;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.FactoryInfoCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.FactoryInfoCache_" + s + ")" : s);
            }
            return value;
        }
        /// <summary>
        /// 厂商添加记录
        /// </summary>
        /// <param name="factoryInfo">厂商实体类</param>
        /// <returns>返回添加成功后的ID</returns>
        public static int Insert(FactoryInfo factoryInfo, string dbkey = "") {
            return Insert(factoryInfo, dbkey, null);
        }
        /// <summary>
        /// 厂商修改记录
        /// </summary>
        /// <param name="factoryInfo">厂商实体类</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>修改是否成功</returns>
        public static bool Update(FactoryInfo factoryInfo, string dbkey = "", string where = "", string[] delCache = null) {
            if (factoryInfo.FactoryID.IsNull()) return false;
            string strSql = new SQL().Update(FactoryInfo._)
                .Set(FactoryInfo._Manufacturers, factoryInfo.Manufacturers)
                .Set(FactoryInfo._IsMoney, factoryInfo.IsMoney)
                .Where("1=1").Where(new Where()
                    .And(FactoryInfo._FactoryID, factoryInfo.FactoryID, Operator.Equal, true)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) : Data.Pool(dbkey).ExecSql(strSql);
            if (value <= 0) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.FactoryInfoCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.FactoryInfoCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// 厂商修改记录
        /// </summary>
        /// <param name="factoryInfo">厂商实体类</param>
        /// <returns>修改是否成功</returns>
        public static bool Update(FactoryInfo factoryInfo, string dbkey = "") {
            return Update(factoryInfo, dbkey, "", null);
        }
        /// <summary>
        /// 厂商修改多条记录
        /// </summary>
        /// <param name="factoryIDList">厂商编号列表，用“,”号分隔</param>
        /// <param name="factoryInfo">厂商实体类</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>修改是否成功</returns>
        public static bool UpdateByIDList(string factoryIDList,  FactoryInfo factoryInfo, string dbkey = "", string where = "", string[] delCache = null) {
            string strSql = new SQL().Update(FactoryInfo._)
                .Set(FactoryInfo._Manufacturers, factoryInfo.Manufacturers)
                .Set(FactoryInfo._IsMoney, factoryInfo.IsMoney)
                .Where("1=1").Where(new Where()
                    .And(FactoryInfo._FactoryID, "(" + factoryIDList + ")", Operator.In)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) : Data.Pool(dbkey).ExecSql(strSql);
            if (value <= 0) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.FactoryInfoCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.FactoryInfoCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// 厂商修改多条记录
        /// </summary>
        /// <param name="factoryIDList">厂商编号列表，用“,”号分隔</param>
        /// <param name="factoryInfo">厂商实体类</param>
        /// <returns>修改是否成功</returns>
        public static bool UpdateByIDList(string factoryIDList,  FactoryInfo factoryInfo, string dbkey = "") {
            return UpdateByIDList(factoryIDList,  factoryInfo, dbkey, "", null);
        }
         /// <summary>
        /// 厂商删除记录
        /// </summary>
        /// <param name="factoryID">厂商编号</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">删除成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(int? factoryID,  string dbkey = "", string where = "", string[] delCache = null) {
            if (factoryID.IsNull()) return false;
            string strSql = new SQL().Delete(FactoryInfo._)
                .Where("1=1").Where(new Where()
                    .And(FactoryInfo._FactoryID, factoryID, Operator.Equal, true)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) :  Data.Pool(dbkey).ExecSql(strSql);
            if (value != 1) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.FactoryInfoCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.FactoryInfoCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// 厂商删除记录
        /// </summary>
        /// <param name="factoryID">厂商编号</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(int? factoryID, string dbkey = "") {
            return DeleteByID(factoryID,  dbkey, "", null);
        }
        /// <summary>
        /// 厂商删除记录
        /// </summary>
        /// <param name="factoryID">厂商编号</param>
        /// <param name="where">修改时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">删除成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(int factoryID,  string dbkey = "", string where = "", string[] delCache = null) {
            return DeleteByID((int?)factoryID,  dbkey, where, delCache);
        }
        /// <summary>
        /// 厂商删除记录
        /// </summary>
        /// <param name="factoryID">厂商编号</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByID(int factoryID, string dbkey = "") {
            return DeleteByID((int?)factoryID,  dbkey, "", null);
        }
        /// <summary>
        /// 厂商删除多条记录
        /// </summary>
        /// <param name="factoryIDList">厂商编号列表，用“,”号分隔</param>
        /// <param name="where">删除时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByIDList(string factoryIDList,  string dbkey = "", string where = "", string[] delCache = null) {
            string strSql = new SQL().Delete(FactoryInfo._)
                .Where("1=1").Where(new Where()
                    .And(FactoryInfo._FactoryID, "(" + factoryIDList + ")", Operator.In)
                ).ToString();
            if (!where.IsNullEmpty()) strSql += where;
            int value = dbkey.IsNullEmpty() ? Data.ExecSql(strSql) :  Data.Pool(dbkey).ExecSql(strSql);
            if (value != 1) return false;
            if (delCache.IsNull()) return true;
            foreach (string s in delCache) {
                if (s.IndexOf("(") == -1 || s.IndexOf("[") == -1)
                    Cache2.Remove(s.IndexOf("Cache_") == -1 ? "GTS.MiniBox.FactoryInfoCache_" + s : s);
                else
                    Cache2.RemoveByPattern(s.IndexOf("Cache_") == -1 ? "(GTS.MiniBox.FactoryInfoCache_" + s + ")" : s);
            }
            return true;
        }
        /// <summary>
        /// 厂商删除多条记录
        /// </summary>
        /// <param name="factoryIDList">厂商编号列表，用“,”号分隔</param>
        /// <param name="where">删除时附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="delCache">修改成功后清理的CACHE key，支持正则</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时使用ConnString连接</param>
        /// <returns>删除是否成功</returns>
        public static bool DeleteByIDList(string factoryIDList, string dbkey = "") {
            return DeleteByIDList(factoryIDList,  dbkey, "", null);
        }
        /// <summary>
        /// 厂商查询所有记录
        /// </summary>
        /// <param name="where">附加条件，统一的前面要加链接符（and、or等等）</param>
        /// <param name="order">排序字段，不加“order by”</param>
        /// <param name="fieldList">设置需要返回的字段</param>
        /// <param name="dbkey">存在数据库连接池中的连接key，为空时随机取连接key</param>
        /// <returns>返回实体记录集</returns>
        public static IList<FactoryInfo> SelectListByAll(string dbkey = "", string where = "", string order = "", string fieldList = "") {
            string cacheNameKey = "GTS.MiniBox.FactoryInfoCache_SelectListByAll_{0}_{1}_{2}".FormatWith(where, order, fieldList);
            return Cache2.Get<IList<FactoryInfo>>(cacheNameKey, cacheSeconds, () => {
                string strSql = string.Empty;
			    if (fieldList.IsNullEmpty()) {
                    strSql = new SQL().From(FactoryInfo._)
                        .Select(FactoryInfo._FactoryID)
                        .Select(FactoryInfo._Manufacturers)
                        .Select(FactoryInfo._IsMoney)
                        .ToString();
                } else {
			        strSql = new SQL().From(FactoryInfo._).Select(fieldList).ToString();
                }
                if (!where.IsNullEmpty()) strSql += " where 1=1 " + where;
                if (!order.IsNullEmpty()) strSql += " order by " + order;
                return Data.Pool(dbkey).GetDbDataReader(strSql).ToList<FactoryInfo>();
            });
        }
        /// <summary>
        /// 厂商查询所有记录
        /// </summary>
        /// <returns>返回实体记录集</returns>
        public static IList<FactoryInfo> SelectListByAll(string dbkey = "") {
            return SelectListByAll(dbkey, "", "", "");
        }
        /// <summary>
        /// 清除厂商查询所有记录的缓存
        /// </summary>
        public static void ClearCacheSelectListByAll() {
            //Cache2.Remove("GTS.MiniBox.FactoryInfoCache_SelectListByAll___");
            Cache2.RemoveByPattern("GTS.MiniBox.FactoryInfoCache_SelectListByAll_(.+?)");
        }
        /// <summary>
        /// 清除厂商所有缓存
        /// </summary>
        public static void ClearCacheAll() {
            Cache2.RemoveByPattern("GTS.MiniBox.FactoryInfoCache_(.+?)");
        }
    }
}

